<template>
  <div class="comment-list">
    <van-cell title="全部评论"></van-cell>
    <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
      <van-cell v-for="(comment ,index) in list" :key="index">
        <comment-item :comment="comment" @reply-click="$emit('reply-click',comment)"></comment-item>
      </van-cell>

    </van-list>
  </div>
</template>

<script>
import { getComments } from '@/api/comment.js'
import commentItem from './comment-item.vue'

export default {
  name: 'commentList',
  data () {
    return {
      loading: false,
      finished: false,
      offset: null,
      limit: 10
      //  list: []
    }
  },
  components: {
    commentItem
  },
  props: {
    source: {
      type: [Number, String, Object],
      required: true
    },
    list: {
      type: Array,
      default: function () {
        return []
      }
    },
    type: {
      type: String,
      default: 'a'
    }
  },
  methods: {
    onLoad () {
      this.getComments()
    },
    async getComments () {
      const params = {
        type: this.type,
        source: this.source.toString(),
        offset: this.offset,
        limit: this.limit
      }
      const { data } = await getComments(params)
      this.list.push(...data.data.results)
      console.log(this.list)
      // 加载状态结束
      this.loading = false
      if (data.data.length) {
        this.offset = data.data.last_id
        console.log('加载下一页')
      } else {
        this.finished = true
        console.log('没有更多了')
      }
    }
  }
}
</script>

<style scoped lang="less">
.comment-list{
padding-bottom: 40px;
}
</style>
